package main

import (
	"fmt"
	"io"
	"net/http"
)

// 测试API v1端点
func main() {
	baseURL := "http://localhost:8080/api/v1"

	fmt.Println("=== 测试API v1端点 ===")

	// 测试分类管理API
	fmt.Println("\n1. 测试获取分类列表")
	testGetCategories(baseURL)

	fmt.Println("\n2. 测试获取分类详情")
	testGetCategory(baseURL, 1)

	fmt.Println("\n3. 测试获取项目列表")
	testGetItems(baseURL)

	fmt.Println("\n4. 测试获取项目详情")
	testGetItem(baseURL, 1)

	fmt.Println("\n5. 测试获取页面数据")
	testGetPageData(baseURL, "home")

	fmt.Println("\n6. 测试获取分类及其项目")
	testGetCategoryItems(baseURL, 1)
}

func testGetCategories(baseURL string) {
	resp, err := http.Get(baseURL + "/categories")
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("状态码: %d\n", resp.StatusCode)
	fmt.Printf("响应: %s\n", string(body))
}

func testGetCategory(baseURL string, id int) {
	resp, err := http.Get(fmt.Sprintf("%s/categories/%d", baseURL, id))
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("状态码: %d\n", resp.StatusCode)
	fmt.Printf("响应: %s\n", string(body))
}

func testGetItems(baseURL string) {
	resp, err := http.Get(baseURL + "/items")
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("状态码: %d\n", resp.StatusCode)
	fmt.Printf("响应: %s\n", string(body))
}

func testGetItem(baseURL string, id int) {
	resp, err := http.Get(fmt.Sprintf("%s/items/%d", baseURL, id))
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("状态码: %d\n", resp.StatusCode)
	fmt.Printf("响应: %s\n", string(body))
}

func testGetPageData(baseURL string, pageName string) {
	resp, err := http.Get(fmt.Sprintf("%s/pages/%s", baseURL, pageName))
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("状态码: %d\n", resp.StatusCode)
	fmt.Printf("响应: %s\n", string(body))
}

func testGetCategoryItems(baseURL string, id int) {
	resp, err := http.Get(fmt.Sprintf("%s/categories/%d/items", baseURL, id))
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("状态码: %d\n", resp.StatusCode)
	fmt.Printf("响应: %s\n", string(body))
}
